<template>
  <view class="content"></view>
</template>
<script setup lang="ts">
import { miniLogin } from '@/api/index'
import { onLoad } from '@dcloudio/uni-app'
import { getQueryString } from '@/utils/hmm'
onLoad(() => {
  init()
})
const init = () => {
  uni.showLoading({
    title: '加载中'
  })
  // 通过code 获取用户信息
  let dataCode = getQueryString('code')
  if (dataCode) {
    uni.removeStorageSync('token')
    uni.removeStorageSync('userInfo')
    miniLogin({ code: dataCode }).then((res: any) => {
      uni.hideLoading()
      uni.setStorageSync('token', res.data.token)
      uni.setStorageSync('userInfo', JSON.stringify(res.data))
      // 获取当前页面的URL
      var currentUrl = window.location
      // 将URL转换为字符串类型
      var urlString = currentUrl.toString()

      // 判断URL中是否包含指定的键名（key）
      if (urlString.includes('?')) {
        // 如果URL中有查询参数部分
        var queryParams = new URLSearchParams(currentUrl.search) // 创建URLSearchParams对象，用于操作查询参数

        // 移除指定的键值对
        if (queryParams.has('code')) {
          // keyName为要删除的键名
          queryParams.delete('code')

          // 重新构造URL
          var newUrl =
            `${currentUrl.protocol}//${currentUrl.host}${currentUrl.pathname}` +
            '?' +
            queryParams.toString()
          //   location.href = newUrl;
          location.replace(newUrl)
        }
      }
    })
  } else {
    uni.switchTab({
      url: '/pages/index/index'
    })
  }
}
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 200rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50rpx;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}
</style>
